package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Override
    public void add(Role role, Integer[] permissionIds,Integer[] menuIds) {
        //向角色表中增加数据
        roleDao.add(role);
        //向中间表中增加数据
        if(permissionIds!=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("permissionId",permissionId);
                map.put("roleId",role.getId());
                roleDao.setRoleAndPermission(map);
            }
        }

        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("menuId",menuId);
                map.put("roleId",role.getId());
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        List<Integer> permissionIds = roleDao.findPermissionIdsByRoleId(id);
        return permissionIds;
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        List<Integer> menuIds = roleDao.findMenuIdsByRoleId(id);
        return menuIds;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //更新角色表
        roleDao.edit(role);
        //删除两张中间表
        roleDao.deletePermissionIdsByRoleId(role.getId());
        roleDao.deleteMenuIdsByRoleId(role.getId());
        //向中间表中增加数据
        if(permissionIds!=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("permissionId",permissionId);
                map.put("roleId",role.getId());
                roleDao.setRoleAndPermission(map);
            }
        }

        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("menuId",menuId);
                map.put("roleId",role.getId());
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        //判断当前角色是否已经关联到用户
        long count = roleDao.findCountByRoleId(id);
        if(count > 0){
            //当前角色已经被关联到用户，不允许删除
            new RuntimeException();
        }
        roleDao.deleteMenuIdsByRoleId(id);
        roleDao.deletePermissionIdsByRoleId(id);
        roleDao.delete(id);
    }

    @Override
    public List<Role> findAll() {
        List<Role> list = roleDao.findAll();
        return list;
    }
}
